package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.lwm.domain.Device;
import com.lwm.domain.FactoryDetail;
import com.lwm.domain.ResponseResult;
import com.lwm.mapper.DeviceMapper;
import com.lwm.mapper.FactoryDetailMapper;
import com.lwm.service.FactoryDetailService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工厂建模明细业务层
 */
@Service
public class FactoryDetailServiceImpl implements FactoryDetailService {
    @Resource
    private FactoryDetailMapper factoryDetailMapper;
    @Resource
    private DeviceMapper deviceMapper;

    /**
     * 保存明细信息
     *
     * @param factoryDetail 工厂建模明细
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> save(FactoryDetail factoryDetail) {
        long detailId = IdUtil.getSnowflakeNextId();
        factoryDetail.setId(detailId); // 设置雪花id
        // 执行插入
        factoryDetailMapper.insert(factoryDetail);
        // 查询出 设备信息，返回
        Device device = deviceMapper.selectById(factoryDetail.getDeviceId());
        Map<String, Object> map = new HashMap<>();
        map.put("device", device);
        map.put("detailId", detailId);
        return new ResponseResult<>(Code.SAVE_OK, "添加成功", map);
    }

    /**
     * 删除
     *
     * @param detailIds 工厂建模id明细
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> remove(List<Long> detailIds) {
        // 空id
        if (null == detailIds || detailIds.isEmpty()) {
            return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_LACK_ID + "工厂建模");
        }
        int i = factoryDetailMapper.deleteByIds(detailIds);
        if (i <= 0) return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_ERR_HINT);
        return new ResponseResult<>(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }
}
